﻿package com.petermacko.utils {
	import flash.events.*;
	import flash.net.SharedObject;

	public class SharedObjectManager extends EventDispatcher
	{
		private var so:SharedObject;

		public function SharedObjectManager(name:String):void {
			this.so = SharedObject.getLocal(name);
			this.so.addEventListener(NetStatusEvent.NET_STATUS, onStatus);
		}
		private function onStatus(e:NetStatusEvent):void {
			switch(e.info.code)	{
				case "SharedObject.Flush.Success":
					this.dispatchEvent(new Event(Event.COMPLETE));
					break;
				case "SharedObject.Flush.Failed":
					this.dispatchEvent(new Event(Event.CANCEL));
					break;
			}
		}
		public function setProperty(obj:Object):void {
			var prop:String;
			for(prop in obj) {
				this.so.data[prop] = obj[prop];
				this.so.flush();
			}
		}
		public function getProperty(name:String):String	{
			return this.so.data[name];
		}
		public function clear():void {
			this.so.clear();
		}
		public override function toString():String {
			return "com.petermacko.utils.SharedObjectManager";
		}
	}
}